package cn.highedu.coolshark_product.security;

import cn.highedu.coolshark_product.pojo.vo.AdminLoginInfoVO;
import cn.highedu.coolshark_product.service.IAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * Spring Security处理认证时使用 获取管理员用户登录详情
 */
@Component // 交给Spring容器管理
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    // 注入IAdminService对象
    @Autowired
    private IAdminService adminService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.debug("Spring Security自用调用loadUserByUsername方法, 参数:{}", username);
        AdminLoginInfoVO loginInfoVO = adminService.getAdminLoginInfo(username);
        if (loginInfoVO != null) {
            log.debug("返回用户{}详情对象", username);
            // 返回用户详情对象
            UserDetails userDetails = new UserDetailsImpl(loginInfoVO);
            log.debug("返回用户{}详情对象的权限信息{}",  username, userDetails.getAuthorities());
            return new UserDetailsImpl(loginInfoVO);
        }
        throw new UsernameNotFoundException("用户名不存在");
    }
}







